package ifi.ro.apvs.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class CCFinder extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GraphPad drawPad;
	private JPanel rightPanel;
	private JScrollPane logScroll;
	private JTextArea txtLog;
	private JPanel topPanel;

	public CCFinder() {
		this.initGui();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CCFinder inst = new CCFinder();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	private void initGui() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
		this.setVisible(true);
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);

		{
			topPanel = new JPanel();
			getContentPane().add(topPanel, BorderLayout.NORTH);
			{
				ImageIcon icoOfile = new ImageIcon(this.getClass().getResource(
						"images/document-open.png"));
				JButton btFromfile = new JButton(icoOfile);
				topPanel.add(btFromfile);
				btFromfile.setToolTipText("Ovrir un fichier de matrice");
				btFromfile.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JFileChooser fc = new JFileChooser();
						fc.setCurrentDirectory(new File("./filexample"));
						int ret = fc.showDialog(CCFinder.this, "Select");
						if (ret == JFileChooser.APPROVE_OPTION) {
							try {
								File f = fc.getSelectedFile();
								FileReader fr = new FileReader(f);
								int c;
								ArrayList<ArrayList<Integer>> matrix = new ArrayList<ArrayList<Integer>>();
								ArrayList<Integer> list = new ArrayList<Integer>();
								while ((c = fr.read()) != -1) {
									if ((char) c != '\n')
										list.add(Integer.parseInt(String
												.valueOf((char) c)));
									else {
										matrix.add(list);
										list = new ArrayList<Integer>();
									}
								}
								if (matrix.size() > 0) {
									drawPad.setMatrix(matrix);
								}
							} catch (Exception e1) {
								JOptionPane.showMessageDialog(CCFinder.this,
										"Le format du fichier est incorrect");
								e1.printStackTrace();
							}
						}
					}
				});
			}
			{
				ImageIcon icoSel = new ImageIcon(this.getClass().getResource(
						"images/transform-move.png"));
				JButton btSelect = new JButton(icoSel);
				topPanel.add(btSelect);
				btSelect.setToolTipText("Selection un sommet");
				btSelect.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						drawPad.select();

					}
				});
			}
			{
				ImageIcon icoAddPoint = new ImageIcon(this.getClass()
						.getResource("images/folders-palettes-22.png"));
				JButton btAddPoint = new JButton(icoAddPoint);

				topPanel.add(btAddPoint);
				btAddPoint.setToolTipText("Ajouter un nouveau sommet");
				btAddPoint.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						drawPad.enableAdd(true);

					}
				});
			}
			{
				ImageIcon icoDel = new ImageIcon(this.getClass().getResource(
						"images/edit-delete.png"));
				JButton btDelPoint = new JButton(icoDel);
				topPanel.add(btDelPoint);
				btDelPoint.setToolTipText("Supprimer un sommet.");
				btDelPoint.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						drawPad.deletePoint();

					}
				});
			}
			{
				ImageIcon icoDrawArc = new ImageIcon(this.getClass()
						.getResource("images/draw-freehand.png"));
				JButton btAddLine = new JButton(icoDrawArc);
				topPanel.add(btAddLine);
				final JPopupMenu mnAddLine = new JPopupMenu();
				JMenuItem item1 = new JMenuItem("Ajouter arrêts");
				item1.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						drawPad.drawLine(false);

					}
				});
				JMenuItem item2 = new JMenuItem("Ajouter arcs");
				item2.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						drawPad.drawLine(true);

					}
				});
				mnAddLine.add(item1);
				mnAddLine.add(item2);
				btAddLine.addMouseListener(new MouseListener() {

					@Override
					public void mouseReleased(MouseEvent arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void mousePressed(MouseEvent e) {
						mnAddLine.show(e.getComponent(), e.getX(), e.getY());

					}

					@Override
					public void mouseExited(MouseEvent arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void mouseEntered(MouseEvent arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void mouseClicked(MouseEvent arg0) {
						// TODO Auto-generated method stub

					}
				});
				btAddLine.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {

					}
				});
			}
			{
				ImageIcon icoClear = new ImageIcon(this.getClass().getResource(
						"images/edit-clear.png"));
				JButton btClear = new JButton(icoClear);
				topPanel.add(btClear);
				btClear.setToolTipText("Effacer le graph");
				btClear.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						drawPad.clearAll();

					}
				});
			}
			{
				ImageIcon icoExec = new ImageIcon(this.getClass().getResource(
						"images/folders-interp-22.png"));
				JButton btCalc = new JButton(icoExec);
				final JPopupMenu mnCalc = new JPopupMenu("calculer");
				JMenuItem ccNo = new JMenuItem(
						"Déterminer des composantes connexes");
				ccNo.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						(new Thread() {
							@Override
							public void run() {
								// TODO Auto-generated method stub
								drawPad.componentConnected(txtLog);
							}
						}).start();

					}
				});
				JMenuItem cFc = new JMenuItem(
						"Déterminer des composantes fortement connexes");
				cFc.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						(new Thread() {
							public void run() {
								drawPad.componentStronglyConnected(txtLog);
							};
						}).start();
					}
				});
				mnCalc.add(ccNo);
				mnCalc.add(cFc);
				topPanel.add(btCalc);
				btCalc.setToolTipText("Déterminer des composantes connexes");
				btCalc.addMouseListener(new MouseListener() {

					@Override
					public void mouseReleased(MouseEvent arg0) {
						// TODO Auto-generated method stub

					}

					@Override
					public void mousePressed(MouseEvent e) {
						mnCalc.show(e.getComponent(), e.getX(), e.getY());
					}

					@Override
					public void mouseExited(MouseEvent arg0) {

					}

					@Override
					public void mouseEntered(MouseEvent arg0) {

					}

					@Override
					public void mouseClicked(MouseEvent arg0) {

					}
				});
			}

		}
		{
			rightPanel = new JPanel();
			getContentPane().add(rightPanel, BorderLayout.EAST);
			rightPanel.setSize(200, -1);
			{
				logScroll = new JScrollPane();
				rightPanel.add(logScroll);
				{
					txtLog = new JTextArea();
					logScroll.setViewportView(txtLog);
					txtLog.setPreferredSize(new java.awt.Dimension(200, this
							.getHeight()));
				}
			}
		}

		{
			drawPad = new GraphPad();
			getContentPane().add(drawPad);
		}
	}

}
